package com.li.sort.arithmetic;

import java.util.Arrays;
import java.util.Random;

public class SelectSort {
	public static final int SIZE = 10;
	public static void main(String[] args) {
		int[] array = new int[SIZE];
		Random random = new Random();
		for(int i = 0;i < SIZE ;i++){
			array[i] = random.nextInt(100);
		}
		
		System.out.println(Arrays.toString(array));
		selectSort(array);
		System.out.println(Arrays.toString(array));
	}
	
	/**
	 * n-1趟就ok
	 * @param array
	 */
	public static void selectSort(int[] array){
		for(int i = 0;i < array.length - 1;i++){
			int min = i;
			for(int j = i + 1;j < array.length;j++){
				if(array[min]>array[j]){
					min = j;
				}
			}
			if(min!=i){
				swap(array,i,min);
			}
		}
	}
	
	public static void swap(int[] array,int i,int j){
		int temp = array[i];
		array[i] = array[j];
		array[j] = temp;
	}
}
